package com.atguigu.gmallre.mock.factory;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmallre.mock.bean.BaseProvince;
import com.atguigu.gmallre.mock.bean.UserInfo;
import com.atguigu.gmallre.mock.config.AppConfig;
import com.atguigu.gmallre.mock.log.AppCommon;
import com.atguigu.gmallre.mock.stage.Stage;
import com.atguigu.mock.util.ConfigUtil;
import com.atguigu.mock.util.RandomBox;
import com.atguigu.mock.util.RandomNum;
import com.google.common.base.CaseFormat;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Builder
@Data
public class UserStageChainFactory {

    Long userSessionCount;




    public List<UserStageChain> produce() {

        List<UserStageChain> userStageChainList = new LinkedList();

        String pathJson = ConfigUtil.loadJsonFile("path.json");
        JSONArray visitPathJsonArr = JSON.parseArray(pathJson);;
        RandomBox<List<String>> visitPathBox = getPathBox(visitPathJsonArr);


        RandomBox<Integer> hourBox = genHourBox();

      //产生多个用户session
        for (int i = 0; i < userSessionCount; i++) {
            UserStageChain userStageChain = ApplicationContextProvider.getBean("userStageChain", UserStageChain.class);


            //访问路径注入
            List<Stage> visitStages = initStages(visitPathBox);

            userStageChain.setVisitStages(visitStages);



            //初始化时间
            Integer hour = hourBox.getRandIntValue();
            Date startDate = getDatetime(hour);
            userStageChain.setStartDatetime(startDate);

            userStageChainList.add(userStageChain);
        }
        return userStageChainList;
    }




    public  List<Stage>  initStages( RandomBox<List<String>>  pathBox){
        if(pathBox==null){
            return null;
        }

        List<Stage>  stages=new ArrayList<>();
        List<String> pathNameList = pathBox.getValue();
        if(pathNameList!=null){
            for (String pathName : pathNameList) {
                Stage stage = getStageByPathName(pathName);
                stages.add(stage);
            }
        }
        return stages;
    }


    private  RandomBox<List<String>> getPathBox(JSONArray  pathJsonArr){


        //制作路径随机箱
        RandomBox.Builder<List<String>> pathRanGroupBuilder = RandomBox.builder();

        for (int i = 0; i < pathJsonArr.size(); i++) {
            JSONObject jsonObject = pathJsonArr.getJSONObject(i);
            List<String> path =   jsonObject.getJSONArray("path").toJavaList(String.class);
            Integer rate =  jsonObject.getInteger("rate")  ;

            pathRanGroupBuilder.add(path, rate);
        }
        return pathRanGroupBuilder.build();

    }

    private Stage getStageByPathName(String pathName) {
        String beanName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, pathName);
        String className = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, pathName);

        Class clazz = null;
        try {

            clazz = Class.forName("com.atguigu.gmallre.mock.stage." + className + "Stage");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("com.atguigu.gmallre.mock.stage." + className + "Stage 未定义");
        }
        Stage stage = (Stage) ApplicationContextProvider.getBean(beanName, clazz);

        return stage;
    }


    private RandomBox<Integer> genHourBox() {

        Integer[] startTimeWeight = AppConfig.start_time_weight;
        RandomBox.Builder<Integer> hourRanBoxBuilder = RandomBox.builder();

        for (int hour = 0; hour < startTimeWeight.length; hour++) {
            hourRanBoxBuilder.add(hour, startTimeWeight[hour]);
        }
        return hourRanBoxBuilder.build();

    }


    private Date getDatetime(Integer hour) {
        String mockDate = AppConfig.mock_date_str;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int minute = RandomNum.getRandInt(0, 59);
        int second = RandomNum.getRandInt(0, 59);
        String hourStr = StringUtils.leftPad(hour + "", 2, "0");
        String miniStr = StringUtils.leftPad(minute + "", 2, "0");
        String secStr = StringUtils.leftPad(second + "", 2, "0");

        String startTime = mockDate + " " + hourStr + ":" + miniStr + ":" + secStr;
        try {
            Date date = simpleDateFormat.parse(startTime);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("日期生成错误！");
        }

    }
}
